package com.joey.cloud.flowable.conf;

import com.joey.cloud.flowable.listener.*;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * flow全局事件监听
 * @author joey
 */

@Configuration
public class FlowableGlobalListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private SpringProcessEngineConfiguration configuration;
    @Autowired
    private MyGlobalFlowableEventListener myGlobalFlowableEventListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //监听的事件
        dispatcher.addEventListener(myGlobalFlowableEventListener,FlowableEngineEventType.PROCESS_COMPLETED);
        dispatcher.addEventListener(myGlobalFlowableEventListener,FlowableEngineEventType.PROCESS_CREATED);
        dispatcher.addEventListener(myGlobalFlowableEventListener,FlowableEngineEventType.TASK_COMPLETED);
        dispatcher.addEventListener(myGlobalFlowableEventListener,FlowableEngineEventType.TASK_CREATED);
    }
}
